package com.itheima.对象操作流;

import java.io.*;

/**
 * @Author 王加鑫
 * @Date 2021/5/20 15:51
 * @Version 1.0
 *
 */
public class Demo {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        method1();
        method2();

    }

    private static void method2() throws IOException, ClassNotFoundException {
        ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream("day13_字符流/user.txt"));
        User user;
        while (true){
            try {
                user = (User) inputStream.readObject();
            } catch (EOFException e) {
                break;
            }
            System.out.println(user);
        }
    }

    /**
     * 序列化过程:将对象写到文件中
     * @throws IOException
     */
    private static void method1() throws IOException {
        User user1 = new User("admin1","123456");
        User user2 = new User("admin2","1234567");
        User user3 = new User("admin3","12345678");
        User user4 = new User("admin4","123456789");
        ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("day13_字符流/user.txt"));
        outputStream.writeObject(user1);
        outputStream.writeObject(user2);
        outputStream.writeObject(user3);
        outputStream.writeObject(user4);
        outputStream.close();

    }


}
